import Vue from 'vue'
import VueRouter from 'vue-router'

import { getToken } from '@/utils/local'

const Layout = () => import('../views/Layout/index')
const Classify = () => import('../views/ShoppingMallPage/Classify')
const Home = () => import('../views/ShoppingMallPage/Home')
const Mine = () => import('../views/ShoppingMallPage/Mine')
const ShoppingCart = () => import('../views/ShoppingMallPage/ShoppingCart')
const Login = () => import('../views/Login/index')
const Serch = () => import('../views/ShoppingMallPage/Serch')
const Details = () => import('../views//Details/index')
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      { path: 'home', component: Home },
      { path: 'mine', component: Mine },
      { path: 'shoppingcart', component: ShoppingCart },
      { path: 'classify', component: Classify }
    ]
  },
  { path: '/login', component: Login },
  { path: '/serch', component: Serch },
  { path: '/details', component: Details },
  { path: '*', component: () => import('../views/NotFound/index.vue') }

]

const router = new VueRouter({
  routes
})
// 白名单
const whitelist = ['/login', '/home']
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token) {
    next()
  } else {
    if (whitelist.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
